package com.buaa.utility.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SecurityMD5 {
	public static String encrypt(String inStr) throws Exception {
        MessageDigest md = null;
        String out = null;
        try {
            md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(inStr.getBytes());
            out = byte2hex(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw e;
        }
        return out;
    }
	
    private static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
               hs = hs + "0" + stmp;
            } else {
               hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
     }
    
    public static void main(String[] args) throws Exception{
    	System.out.println("111111:"+SecurityMD5.encrypt("111111"));
    	System.out.println("123:"+SecurityMD5.encrypt("123"));
    }
}
